package com.joey.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author hyy
 **/
@Slf4j
public class SynchronizedObjectExample {

    public static void main(String[] args) {
        SynchronizedObjectExample example = new SynchronizedObjectExample();
        SynchronizedObjectExample example2 = new SynchronizedObjectExample();
        ExecutorService executorService = Executors.newCachedThreadPool();

        executorService.execute(() -> {
            example.test(1);
//            example.test2(1);
        });
        executorService.execute(() -> {
//            example2.test(2);
            example2.test2(2);
        });

        executorService.shutdown();
    }

    /**
     * 限制同一对象调用
     * @param j
     */
    public void test(int j){
        synchronized (SynchronizedObjectExample.class){
            for (int i=0;i<10;i++){
                log.info("test1 - {} - {}", j, i);
            }
        }
    }

    /**
     * 限制同一对象调用
     * @param j
     */
    public static synchronized void test2(int j){
        for (int i=0;i<10;i++){
            log.info("test2 -{}- {}",j,i);
        }
    }

}
